var Post = require('../../models/post/post')
var Active = require('../../models/active/active')
var User = require('../../models/user/user')

module.exports = {

    getPost: function (req, res) {
        var query = req.query
        var skip = req.params.skip
        var limit = + req.params.limit

        var filter = {}
        if (query) {
            filter = query
        }

        Post.find(query).sort({updatedAt: -1}).skip(skip).limit(limit).exec(function(err, posts) {
          if (err) {
              console.log('postController getPostList fail. err = ' + err)
              return res.json({code: 177, msg: '获取失败'})
          }
          if (posts.length == 0) {
              return res.json({code: 112, msg: '没有数据'})
          }
          return res.json({code: 100, msg: '获取成功', data: {list: posts}})
        })
    },
    getOne: function(req, res) {
        var id = req.params.id

        Post.findById(id, function (err, post) {
            if (err) {
                console.log('postController getOne fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            if (!post) {
                return res.json({code: 112, msg: '没有数据'})
            }
            return res.json({code: 100, msg: '获取成功', data: {post: post}})
        })

    },
    add: function (req, res) {
        var body = req.body

        new Post(body).save(function (err, post) {
            if (err) {
                console.log('postController add fail. err = ' + err)
                return res.json({code3: 177, msg: '添加失败'})
            }
            var active = new Active({
                type: 1,
                active: {
                    handler: post.handler,
                    activity: {
                        type: 2,
                        post: {
                            title: post.title,
                            id: post._id
                        }
                    }
                }
            })
            active.save(function (err, active) {
                if (err) {
                    console.log('postController add fail. err = ' + err)
                    return res.json({code: 177, msg: '操作失败'})
                }
                return res.json({code: 100, msg: '发表成功', data: {post: post}})
            })
            
        })
    },
    remove: function(req, res) {
        var id = req.params.id

        Post.remove({_id: id}, function (err) {
            if (err) {
                console.log('postController remove fail. err = ' + err)
                return res.json({code: 177, msg: '删除失败'})
            }
            return res.json({code: 100, msg: '删除成功'})
        })

    },
    addCollect: function(req, res) {

        var id = req.params.id

        Post.findOneAndUpdate({_id: id}, {collect: {$push: req.body.handler}}, {upsert: true, new: true}, function (err, post) {
            if (err) {
                console.log('postController addCollect fail. err = ' + err)
                return res.json({code: 177, msg: '收藏失败'})
            }
            User.findOneAndUpdate({_id: req.body.handler._id}, {collectPost: {$push: req.body.post}}, {upsert: true, new: true}, function (err, user) {
                if (err) {
                    console.log('postController addCollect fail. err = ' + err)
                    return res.json({code: 177, msg: '收藏失败'})
                }
                return res.json({code: 100, msg: '收藏成功'})
            })
        })

    },
    addComment: function (req, res) {
        var id = req.params.id
        var comment = req.body
        var commentId = req.query.commentId

        Post.findById(id, function (err, post) {
            if (err) {
                console.log('postController addComment fail. err = ' + err)
                return res.json({code: 177, msg: '未找到帖子'})
            }
            if (!commentId) {
                post.comments.push(comment)
            } else {
                console.log(commentId)
                for (var i = 0; i < post.comments.length; i ++) {
                    if (commentId == post.comments[i]._id.toString()) {

                        post.comments[i].comments.push(comment)
                    }
                }
            }
            post.save(function (err, post) {
                if (err) {
                    console.log('postController addComment fail. err = ' + err)
                    return res.json({code: 177, msg: '评价失败'})
                }
                return res.json({code: 100, data: {post: post}})
            })
        })
    }
}
